package service.Impl;

import DAO.AdminDAO;
import DAO.BlackListDAO;
import DAO.ItemsDAO;
import DAO.UserDAO;
import DAO.impl.AdminDAOImpl;
import DAO.impl.BlackListDAOImpl;
import DAO.impl.ItemsDAOImpl;
import DAO.impl.UserDAOImpl;
import bean.Admin;
import bean.BlackList;
import bean.Items;
import bean.Users;
import service.AdminService;
import utils.JdbcUtils;

import java.sql.Connection;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

/**
 * @author 栋
 * @date 2022/12/5
 * @description
 */
public class AdminServiceImpl implements AdminService {
    AdminDAO adminDAO=new AdminDAOImpl();
    ItemsDAO itemsDAO=new ItemsDAOImpl();
    UserDAO userDAO=new UserDAOImpl();
    BlackListDAO blackListDAO=new BlackListDAOImpl();
    @Override
    public Admin login(String account, String pwd) {
        Connection conn = null;
        try {
            conn = JdbcUtils.getConnection();
            Admin adminByAcc = adminDAO.getAdminByAcc(conn,account);
            if(adminByAcc.getAdmin_pwd().equals(pwd)){
                return adminByAcc;
            }else{
                return null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.closeResoure(conn);
        }
        return null;
    }

    @Override
    public List<Items> showShenHeItems() {
        List<Items> shenHeItems = null;
        Connection conn =null;
        try {
            conn=JdbcUtils.getConnection();
            shenHeItems = itemsDAO.getShenHeItems(conn);

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.closeResoure(conn);
        }
        return shenHeItems;

    }

    @Override
    public void updateItemsInfor(String item_id,String admin_id, String is_qualified) {
        Connection conn = null;
        try {
            conn = JdbcUtils.getConnection();
            Date date=Date.valueOf(LocalDate.now());
            Time time=Time.valueOf(LocalTime.now());
            Items item = itemsDAO.getItemById(conn, item_id);
            item.setAdmin_id(admin_id);
            item.setAudit_date(date);
            item.setAudit_time(time);
            item.setIs_qualified(is_qualified);
            itemsDAO.update_item(conn,item);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.closeResoure(conn);
        }


    }

    @Override
    public List<Users> getallUsers() {
        Connection conn = null;
        List<Users> all_users = null;
        try {
            conn = JdbcUtils.getConnection();
            all_users = userDAO.get_all_users(conn);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.closeResoure(conn);
        }

        return all_users;
    }

    @Override
    public boolean addToBlackList(BlackList b) {
        Connection conn = null;
        try {
            conn = JdbcUtils.getConnection();
            BlackList blackById = blackListDAO.getBlackById(conn, b.getUser_id());
            if(blackById!=null){
                return false;
            }else{
                blackListDAO.add_black(conn,b);
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.closeResoure(conn);
        }
        return false;

    }

    @Override
    public List<BlackList> getAllBlack() {
        Connection conn = null;
        List<BlackList> all_black = null;
        try {
            conn = JdbcUtils.getConnection();
            all_black = blackListDAO.get_all_black(conn);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.closeResoure(conn);
        }

        return all_black;
    }

    @Override
    public void jiechulahei(String user_id) {
        Connection conn = null;
        try {
            conn = JdbcUtils.getConnection();
            blackListDAO.delete_black(conn,user_id);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.closeResoure(conn);
        }


    }
}
